/*** 
 * @Date: 2020-07-10 14:12:36
 * @LastEditors: leehave
 * @LastEditTime: 2020-09-09 13:38:36
 */
/***
 * @Date: 2020-07-10 14:12:36
 * @LastEditors: leehave
 * @LastEditTime: 2020-07-10 14:12:36
 */
export function param2Obj(url) {
  const search = url.split("?")[1];
  if (!search) {
    return {};
  }
  return JSON.parse(
    '{"' +
      decodeURIComponent(search)
        .replace(/"/g, '\\"')
        .replace(/&/g, '","')
        .replace(/=/g, '":"')
        .replace(/\+/g, " ") +
      '"}'
  );
}
export function getState(state){
  let states = {
    '0': 'normal',
    '1': 'loss',
    '2': 'auto_loss'
  }
  
  // let obj = {
  //   className: state['state'],
  //   state_label: stateLabel['state']
  // }
  return states[state]
}
export function getStateLabel(state){
  let stateLabel = {
    '0': '正常',
    '1': '挂失',
    '2': '自动挂失'
  }
  return stateLabel[state]
}

export function renderAnimate(status) {
  if ([2, 3, 4, 5].indexOf(status) !== -1) {
    return 'box'
  } else {
    return 'boxNoAnimation'
  }
}

export function chargePayType(tp) {
  let type = {
    'pointpay': '积分支付',
    'quickpay': '快捷支付',
    'wechat': '微信支付',
    'wallet': '余额支付'
  }
  return type[tp]
}

export function payType(tp) {
  let type = {
    'wallet': '钱包',
    'wechat': '微信',
    'card': '离线卡'
  }
  return type[tp]
}

export function statusType(tp) {
  let status = {
    '0': '未回调',
    '1': '支付成功',
    '2': '支付失败',
    '3': '退款',
  }
  return status[tp]
}

export function chargeOrderType(tp) {
  let type = {
    '0': '正在充电中',
    '1': '成功',
    '2': '失败',
    '3': '余额不足',
    '4': '短时间取消充电'
  }
  return type[tp]
}

export function getDateTime(value) {
  //不能使用 new Date()
  if(typeof value == 'object' && value == null){
    return ''
  }
  let time = new Date(value);
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  let date = time.getDate();
  let hour = time.getHours();
  let minute = time.getMinutes();
  let second = time.getSeconds();
  month = month < 10 ? "0" + month : month;
  date = date < 10 ? "0" + date : date;
  hour = hour < 10 ? "0" + hour : hour;
  minute = minute < 10 ? "0" + minute : minute;
  second = second < 10 ? "0" + second : second;
  return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
}

 // 获取URL中的参数值
 export function GetQueryString(url) {
    let reg = new RegExp("(^|&)" + url + "=([^&]*)(&|$)");
    let r = window.location.search.substr(1).match(reg);
    if (r != null){
        return unescape(r[2]);
    }
    return null;
}

// object转URL参数
export function objectConvertToQuery(payload = {}){
  let queryStr = '?', i = Object.keys(payload).length
  if (i == 0 && JSON.stringify(payload) == '{}') {
     return queryStr = ''
  }
  Object.keys(payload).forEach((key, index) => {
      if ( i === (index + 1)) {
          queryStr += (key + '=' + payload[key])
      } else {
          queryStr += (key + '=' + payload[key] + '&')
      }
  })
  return queryStr
}

//提取出来的公共方法
/*
* 判断是否IOS环境
* */
export function isIOS () {
  let isIphone = navigator.userAgent.includes('iPhone')
  let isIpad = navigator.userAgent.includes('iPad')
  return isIphone || isIpad
}

